package org.example.d8_stream;

import org.example.d6_map_impl.Student;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest4 {
    public static void main(String[] args) {

        List<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 26,  172.5);
        Student s2 = new Student("蜘蛛精", 26,  172.5);
        Student s3 = new Student( "紫霞", 23,  167.6);
        Student s4 = new Student("白晶晶", 25,   169.0);
        Student s5 = new Student("牛魔王", 35,  183.3);
        Student s6 = new Student("牛夫人", 34,   168.5);
        Collections.addAll(students, s1, s2, s3, s4, s5, s6);

        //需求1：请计算出身高超过168 的学生有几人
        long size = students.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println(size);

        //需求2:请找出身高最高的对象，并输出
        Student s = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(s);

        //需求3：请找出最矮的学生对象，并输出
        Student b = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(b);

        //需求4：请找出身高超过170的学生对象，并放到一个新集合中去返回
        //流只能用一次哦
        List<Student> students1 = students.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toList());
        System.out.println(students1);

        Set<Student> students2 = students.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toSet());
        System.out.println(students2);

        //需求5：请找出身高超过170的学生对象，并把对象的名字和身高，存入到一个map集合里并返回
        Map<String, Double> map = students.stream().filter(a -> a.getHeight() > 170)
                .distinct().collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
        System.out.println(map);

       // Object[] array = students.stream().filter(a -> a.getHeight() > 170).toArray();
        Student[] array = students.stream().filter(a -> a.getHeight() > 170).toArray(len->new Student[len]);
        System.out.println(Arrays.toString(array));

    }

}
